package org.example.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import org.example.domain.SysDept;
import org.example.service.SysDeptService;
import org.example.service.SysDeptService;

import java.util.List;

/**
 * @author : Guosir
 * @date : 2024-06-26 16:49
 * @description :
 **/
public class DeptListener implements ReadListener<SysDept> {

    private static final  int BATCH_COUNT = 3;

    private SysDeptService sysDeptService;

    private List<SysDept> cacheDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

    public DeptListener(SysDeptService sysDeptService){
        this.sysDeptService = sysDeptService;
    }



    @Override
    public void invoke(SysDept sysDept, AnalysisContext analysisContext) {
        System.out.println("解析出来的数据"+sysDept);
        cacheDataList.add(sysDept);

        if(cacheDataList.size()>=BATCH_COUNT){
            System.out.println("list:"+cacheDataList);
            sysDeptService.saveDeptBath(cacheDataList);

            //重新赋值
            cacheDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        System.out.println("解析完成");

        if(cacheDataList.size()>0){
            //最后一次添加
            sysDeptService.saveDeptBath(cacheDataList);
            System.out.println("list:"+cacheDataList);
        }
    }
}
